<template>
	<view>
		<!-- <back></back> -->
		<view class="tabs">
			<u-tabs ref="tabs" :current="current" :list="tabList" bar-height="6" bar-width="50" active-color="#606DCE"
				font-size="28" inactive-color="#888888" :is-scroll="false" @change="change"></u-tabs>
		</view>
		<mescroll-body :sticky="true" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
			<view class="all">
				<view class="card" v-for="(item,index) in flowList" :key="index"
					@click="$func.jump('/yCreate/pages/orderDetail?id='+item.id + '&showPayType= 1')">
					<view class="shop-message u-flex">
						<image src="@/static/yCreate/shopIcon.png" mode="widthFix"></image>
						<view>{{item.shop.shopname}}</view>
						<view class="standing standing2 u-flex-center" v-if="item.shop.isself==1">平台</view>
						<view class="standing u-flex-center" v-else>商家</view>
					</view>
					<view class="top u-flex-center u-row-between">
						<view>订单号：{{item.order_no}}</view>
						<view class="state" style="color: #FF0000" v-if="item.state==1">待支付</view>
						<view class="state" v-if="item.state==2">待发货</view>
						<view class="state" style="color: #FF0000" v-if="item.state==3">待收货</view>
						<view class="state" v-if="item.state==7">已取消</view>
						<view class="state" style="color: #606DCE;" v-if="item.state==6">已完成</view>
						<view class="state" style="color: #606DCE;" v-if="item.state==4">待评价</view>
					</view>
					<view class="u-flex-center u-row-left u-m-b-20" v-for="(_item,_index) in item.goods" :key="_index">
						<image :src="_item.image" mode="aspectFill" class="head-img"></image>
						<view class="cont">
							<view class="name u-line-2" style="line-height: 1.2rem">{{_item.title}}</view>
							<view class="cate">规格：{{_item.difference}}</view>
							<view class="u-flex u-row-between u-m-t-20">
								<view class="price"><text>￥</text>{{_item.price}}</view>
								<view class="number">×{{_item.number}}</view>
							</view>
						</view>
					</view>
					<!-- v-if="current==2" -->
					<view class="u-border-top u-m-t-30 u-p-t-20">
						<view class="u-flex u-row-right u-p-b-20">运费：{{item.pay.freight_price}}</view>
						<view class="u-flex-center u-row-between"
							v-if="item.state==1||item.state==2||item.state==3||item.state==4">
							<view></view>
							<!-- <view class="time" @click="cancel(item.id)" v-if="item.state==1">剩余支付时间：23:59:59</view> -->
							<!-- <text>预计5天后送达</text> -->
							<view class="u-flex-center">
								<view class="button button1" @click.stop="cancel(item.id,item.state)"
									v-if="item.state==1||item.state==2">取消订单</view>
								<view class="button" @click.stop="pay(item.id)" v-if="item.state==1">立即付款</view>
								<!-- <view class="button button_wl" @click.stop="$func.jump('/yCreate/pages/orderDetail?id='+item.id + '&showType= 1')" v-if="item.state==3 && item.id != 533">查看物流</view> -->
								<view class="button button_wl" @click.stop="getDetail(item.id)" v-if="item.state==3">查看物流</view>
								<view class="button" @click.stop="confirm(item.id)" v-if="item.state==3">确认收货</view>
								<view class="button" @click.stop="$func.jump('/wNew/pages/evaluate?id=' + item.id)"
									v-if="item.state==4">待评价</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			
		</mescroll-body>
		
		<u-popup v-model="show" mode="bottom" border-radius="25" :height="detail.is_qczx !== 1 ? '70%' : '35%'" closeable safe-area-inset-bottom>
			<view class="coupon">
				<view class="cou_tit">物流信息</view>
				<view style="height: 100rpx;"></view>
				<view class="u-p-30">
					<view class="u-flex u-row-between">
						<view class="name u-flex">
							<image :src="logistics.kuaidi ? logistics.kuaidi.logo :''" mode="aspectFill" v-if="logistics.kuaidi ? logistics.kuaidi.logo :false"></image>
							<view v-if="detail.is_qczx == 1">
								<view style="padding:10px 0;font-size:16px;">
									<text class="titleL">配送方式：</text>
									<text>千创专线</text>
								</view>
								<view style="padding:10px 0;font-size:16px;">
									<text class="titleL">送达时间：</text>
									<text>
										{{detail.sdsj}}
									</text>
								</view>
							</view>
							<view v-else>{{  logistics.kuaidi ? logistics.kuaidi.name : ''  }} <text class="u-m-l-24">{{logistics.express_no}}</text>
							</view>
						</view>
						<view class="copy" v-if="detail.is_qczx != 1" @click="paste(logistics.order_no)">复制</view>
					</view>
					<view class="u-p-20" v-if="detail.is_qczx != 1">
						<u-time-line>
							<u-time-line-item v-for="item in logistics.express" :key="item.time">
								<template v-slot:node v-if="item.status">
									<view class="u-node"></view>
								</template>
								<template v-slot:content v-if="item.status">
									<view>
										<view class="u-order-title u-flex">
											<text class="u-m-r-15">{{item.status}}</text>
											<view class="u-order-time ">{{item.time}}</view>
										</view>
										<!-- <view class="u-order-desc">您的快递。。。。【深圳市】已揽收，揽收人：加加加（<text>1836745744</text>）</view> -->
										<view class="u-order-desc">{{item.context}}</view>
									</view>
								</template>
							</u-time-line-item>
						</u-time-line>
					</view>
				</view>
				<view style="height: 10rpx;"></view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	//#ifdef  H5
	var wx = require('jweixin-module')
	//#endif
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				tabList: [{
					name: '全部'
				}, {
					name: '待支付'
				}, {
					name: '待发货'
				}, {
					name: '待收货'
				}, {
					name: '待评价'
				}],
				flowList: [],
				current: 0,
				detail: {},
				logistics: {},
				show: false
			};
		},
		onShow() {
			this.mescroll.resetUpScroll();
		},
		methods: {
			// 物流
		 	async getDetail(id) {
				await this.$u.get('/api/wanlshop/order/getOrderInfo', {
					id: id
				}).then(res => {
					this.detail = res.data
				})
				await this.$u.post('/api/wanlshop/order/getLogistics', {
					id: id
				}).then(res => {
					this.logistics = res.data
				})
				this.show = true;
			},
			paste(value) {
				uni.setClipboardData({
					data: value
				});
			},
			cancel(id, type) {
				let path
				if (type == 1) {
					path = '/api/wanlshop/order/cancelOrder'
				} else if (type == 2) {
					path = '/api/wanlshop/order/cancelOrder_dfh'
				}
				this.$u.post(path, {
					id: id
				}).then(res => {
					if (res.code == 1) {
						this.mescroll.resetUpScroll();
					} else {
						this.$u.toast(res.msg)
					}
				})
			},
			pay(id) {
				let that = this
				let method = ''
				//#ifdef  H5
				method = 'mp'
				//#endif
				this.$u.post('/api/wanlshop/pay/payment', {
					code: null,
					method: 'app',
					order_id: id,
					order_type: 'goods',
					type: 'wechat'
				}).then(res=>{
					// this.$u.toast(res.msg)
					if(res.code==1) {
						// #ifdef H5
						wx.config({
							debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
							appId: res.data.appId, // 必填，公众号的唯一标识
							timestamp: res.data.timeStamp, // 必填，生成签名的时间戳
							nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
							signature: res.data.paySign, // 必填，签名
							jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表
						})
						// #endif
						let orderInfo = JSON.parse(res.data);
						// #ifdef H5
						wx.ready(function() {
						// #endif
						var wxpaySev = null;
								plus.payment.getChannels(function(channels){
									for (var i in channels) {
										var channel = channels[i];
										if (channel.id === 'wxpay') {
											wxpaySev = channel;
										}
									}
									//发起支付
									plus.payment.request(wxpaySev, orderInfo, function(result) {
										var rawdata = JSON.parse(result.rawdata);
											uni.showToast({
												title: '支付成功'
											})
											// setTimeout(() => {
											// 	uni.reLaunch({
											// 		url: '/yCreate/pages/goodsOrder'
											// 	})
											// }, 1500)
										}, function(e) {
											console.log("支付失败：" + JSON.stringify(e));
											uni.showToast({
												title: '支付失败',
												icon: 'none'
											})
											// setTimeout(() => {
											// 	uni.reLaunch({
											// 		url: '/yCreate/pages/goodsOrder'
											// 	})
											// }, 1500)
										});
									}, function(e){
										console.log("获取支付渠道失败：" + JSON.stringify(e));
										uni.showToast({
											title: '支付失败',
											icon: 'none'
										})
									});
						return
							wx.chooseWXPay({
								timestamp: res.data.timeStamp, // 时间戳
								nonceStr: res.data.nonceStr, // 随机数
								package: res.data.package, //
								signType: res.data.signType,
								paySign: res.data.paySign, // 签名
								success: function() {
									uni.showToast({
										title: '支付成功'
									})
									that.mescroll.resetUpScroll();
								},
								cancel: function() {
									uni.showToast({
										title: '取消支付',
										icon: 'none'
									})
									that.mescroll.resetUpScroll();
								},
								fail: function() {
									uni.showToast({
										title: '支付失败',
										icon: 'none'
									})
									that.mescroll.resetUpScroll();
								}
							})
						// #ifdef H5
						})
						// #endif
					}
				})
			},
			confirm(id) {
				this.$u.post('/api/wanlshop/order/confirmOrder', {
					id: id
				}).then(res => {
					if (res.code == 1) {
						this.mescroll.resetUpScroll();
					} else {
						this.$u.toast(res.msg)
					}
				})
			},
			upCallback(page) {
				this.mescroll.optDown.use = false;
				this.mescroll.optUp.textNoMore = '-已经到底啦-'
				this.$u.get('/api/wanlshop/order/getOrderList', {
					page: page.num,
					state: this.current
				}).then(res => {
					this.mescroll.endBySize(res.data.data.length, res.data.total);
					if (page.num == 1) this.flowList = []; //如果是第一页需手动制空列表
					this.flowList = this.flowList.concat(res.data.data); //追加新数据
				}).catch(() => {
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			change(index) {
				this.current = index;
				this.mescroll.resetUpScroll();
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f6f6f6;
	}

	.tabs {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.all {
		padding: 0 24rpx;

		.card {
			width: 100%;
			margin-top: 18rpx;
			padding: 30rpx 20rpx;
			background: #FFFFFF;
			border-radius: 10rpx;

			.shop-message {
				font-size: 32rpx;

				image {
					width: 28rpx;
					height: auto;
					margin-right: 10rpx;
				}

				.standing {
					margin-left: 18rpx;
					width: 64rpx;
					height: 32rpx;
					background: #F43000;
					border-radius: 4rpx;
					font-size: 24rpx;
					color: #FFFFFF;
				}

				.standing2 {
					background: #606DCE;
				}
			}

			.top {
				margin: 30rpx 0;
				font-size: 24rpx;

				.state {
					color: #888;
				}
			}

			.head-img {
				width: 160rpx;
				height: 160rpx;
				border-radius: 10rpx;
				margin-right: 20rpx;
				flex-shrink: 0;
			}

			.cont {
				width: 100%;

				.name {
					margin-bottom: 12rpx;
					font-size: 28rpx;
				}

				.cate {
					width: fit-content;
					width: -webkit-fit-content;
					width: -moz-fit-content;
					padding: 3rpx;
					background: #fafafa;
					font-size: 22rpx;
					border-radius: 5rpx;
				}

				.content {
					font-size: 24rpx;
					color: #888888;
				}

				.bot {
					margin-top: 40rpx;
					font-size: 26rpx;
					color: #888;

					.price {
						font-family: PingFang SC;
						font-weight: 500;
						color: #DA0000;
					}

					text {
						font-size: 32rpx;
					}
				}

				.price {
					font-size: 40rpx !important;
					font-weight: 550;
					color: #EF5205;

					text {
						font-weight: 500;
						font-size: 20rpx;
					}
				}

				.number {
					font-size: 28rpx;
					font-weight: bold;
					color: #222222;
				}
			}

		}
	}

	.time {
		font-size: 24rpx;
		color: #888888;
	}

	.button {
		margin-left: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 140rpx;
		height: 52rpx;
		background: linear-gradient( 98deg, #5D64CA 0%, #6F9BDF 100%);
		border-radius: 40rpx;
		font-size: 24rpx;
		color: #FFFFFF;
	}

	.button1 {
		border: 1rpx solid #888888;
		background: #fff;
		color: #888888;
	}
		.coupon {
		height: 100%;

		.cou_tit {
			padding: 30rpx 0;
			text-align: center;
			font-size: 32rpx;
			color: #FFFFFF;
			position: fixed;
			top: 0;
			width: 100%;
			z-index: 3;
			background-color: #606DCE;
		}

		.name {
			font-size: 28rpx;
			color: #333333;

			image {
				margin-right: 8rpx;
				width: 44rpx;
				height: 44rpx;
				border-radius: 254rpx;
			}
		}

		.copy {
			font-size: 28rpx;
			color: #333333;
		}

		.cou_item {
			margin-bottom: 40rpx;
			position: relative;
			width: 100%;
			height: 210rpx;
			overflow: hidden;
			border-radius: 30rpx;

			.cou_grey {
				position: absolute;
				top: 0;
				bottom: 0;
				left: 0;
				right: 0;
				z-index: 2;
				background: rgba(76, 76, 76, 0.5);
			}

			.left {
				width: 471rpx;
				height: 210rpx;
				background: #fff;
				padding: 27rpx 40rpx 0rpx 30rpx;

				.code {
					font-size: 22rpx;
					color: #666666;
				}

				.cou_price {
					margin-right: 25rpx;
					color: #E55D23;
					font-size: 88rpx;
					font-weight: 550;

					text {
						margin-top: 32rpx;
						font-size: 26rpx;
						margin-right: 10rpx;
					}
				}

				.title {
					font-size: 26rpx;
					font-weight: bold;
					color: #333333;
				}

				.tips {
					margin-top: 10rpx;
					width: 154rpx;
					height: 31rpx;
					background: #E55D23;
					font-size: 20rpx;
					color: #FFFFFF;
				}

				.time {
					height: 20rpx;
					font-size: 22rpx;
					color: #666666;
					margin-top: 35rpx;
				}
			}

			.status {
				width: 220rpx;
				height: 210rpx;
				position: absolute;
				right: 0;
				top: 50%;
				transform: translateY(-50%);
				font-size: 30rpx;
				font-weight: bold;
				color: #fff;
				background: #606DCE;
			}
		}
	}
	.button_wl{
		border: 1px solid #F43000 !important;
		background: rgba(76, 76, 76, 0);
		color: #F43000;
	}
	/deep/.uicon-close::before{
		color: white;
	}
</style>